package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.entity.RefundAnalysis;
import pers.cj.pdos.mapper.RefundAnalysisMapper;
import pers.cj.pdos.service.IRefundAnalysisService;
import pers.cj.pdos.utils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2020-03-25
 */
@Service
public class RefundAnalysisServiceImpl extends ServiceImpl<RefundAnalysisMapper, RefundAnalysis> implements IRefundAnalysisService {
    @Autowired
    RefundAnalysisMapper refundAnalysisMapper;

    @Override
    public Map getTotalData(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.getTotalData(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public List<RefundAnalysis> listStoreData(String companyId,Integer page,Integer limit, String storeId, String houseId, Date date1, Date date2){
            return refundAnalysisMapper.listStoreData(companyId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2);
    }

    @Override
    public List<RefundAnalysis> listHouseData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2){
        return refundAnalysisMapper.listHouseData(companyId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2);
    }


    @Override
    public List<RefundAnalysis> listGoodsData(String companyId,
                                                   Integer page,
                                                   Integer limit,
                                                   String storeId,
                                                   String houseId,
                                                   String customId,
                                                   Date date1,
                                                   Date date2,
                                                   String typeId,
                                                   Boolean filterZero,
                                                   String search) {
        return refundAnalysisMapper.listGoodsData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, customId, date1, date2, typeId, filterZero, search);
    }

    @Override
    public Long countGoodsData(String companyId,
                               String storeId,
                               String houseId,
                               String customId,
                               Date date1,
                               Date date2,
                               String typeId,
                               Boolean filterZero,
                               String search) {
        return refundAnalysisMapper.countGoodsData(companyId, storeId, houseId, customId, date1, date2, typeId, filterZero, search);
    }

    @Override
    public List<RefundAnalysis> listCustomData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2, String customTypeId, Boolean filterZero, String search) {
        List<RefundAnalysis> list = refundAnalysisMapper.listCustomData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, date1, date2, customTypeId, filterZero, search);

        BigDecimal total = refundAnalysisMapper.getTotalCustom(companyId, storeId, houseId, date1, date2, customTypeId, filterZero, search);
        if (!Utils.isEmpty(list) && total.compareTo(new BigDecimal(0))!=0) {
            List<RefundAnalysis> newList = new ArrayList<>();
            for (RefundAnalysis entity:list) {
                entity.setRatio(entity.getAmount().multiply(new BigDecimal(100)).divide(total,4, RoundingMode.HALF_UP));
                newList.add(entity);
            }
            return newList;
        }

        return list;
    }

    @Override
    public Long countCustomData(String companyId, String storeId, String houseId, Date date1, Date date2, String customTypeId, Boolean filterZero, String search) {
        return refundAnalysisMapper.countCustomData(companyId, storeId, houseId, date1, date2, customTypeId, filterZero, search);
    }

    @Override
    public BigDecimal getTotalCustom(String companyId, String storeId, String houseId, Date date1, Date date2, String customTypeId, Boolean filterZero, String search) {
        return refundAnalysisMapper.getTotalCustom(companyId, storeId, houseId, date1, date2, customTypeId, filterZero, search);
    }

    @Override
    public List<RefundAnalysis> listOrderData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.listOrderData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, date1, date2);
    }

    @Override
    public Long countOrderData(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.countOrderData(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public Map getHighGoods(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.getHighGoods(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public Map getHighCustom(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.getHighCustom(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public List<RefundAnalysis> listGoodsDetailDataByOrder(String companyId, String goodsId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2, String search) {
        return refundAnalysisMapper.listGoodsDetailDataByOrder(companyId, goodsId,  Utils.genPage(page,limit), limit, storeId, houseId, date1, date2,search);
    }

    @Override
    public Long countGoodsDetailDataByOrder(String companyId, String goodsId, String storeId, String houseId, Date date1, Date date2,String search) {
        return refundAnalysisMapper.countGoodsDetailDataByOrder(companyId, goodsId, storeId, houseId, date1, date2,search);
    }

    @Override
    public List<RefundAnalysis> listGoodsDetailDataByCustom(String companyId, String goodsId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2, String search) {
        return refundAnalysisMapper.listGoodsDetailDataByCustom(companyId, goodsId,  Utils.genPage(page,limit), limit, storeId, houseId, date1, date2,search);
    }

    @Override
    public Long countGoodsDetailDataByCustom(String companyId, String goodsId, String storeId, String houseId, Date date1, Date date2, String search) {
        return refundAnalysisMapper.countGoodsDetailDataByCustom(companyId, goodsId, storeId, houseId, date1, date2,search);
    }

    @Override
    public Map getMaxMinDate(String companyId, String goodsId, String customId, String storeId, String houseId) {
        return refundAnalysisMapper.getMaxMinDate(companyId, goodsId, customId, storeId, houseId);
    }

    @Override
    public List<Map> getTotalByYear(String companyId, String goodsId, String customId, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.getTotalByYear(companyId, goodsId, customId, storeId, houseId, date1, date2);
    }

    @Override
    public List<Map> getTotalByMonth(String companyId, String goodsId, String customId, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.getTotalByMonth(companyId, goodsId, customId, storeId, houseId, date1, date2);
    }

    @Override
    public List<Map> getTotalByDate(String companyId, String goodsId, String customId, String storeId, String houseId, Date date1, Date date2) {
        return refundAnalysisMapper.getTotalByDate(companyId, goodsId, customId, storeId, houseId, date1, date2);
    }


}
